package com.bawei.shopping.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter

import com.bawei.shopping2.R
import com.bawei.shopping2.adapter.CategoryAdapter
import com.bawei.shopping2.adapter.GoodsAdapter
import com.bawei.shopping2.adapter.MainCategoryAdapter
import com.bawei.shopping2.databinding.FragmentOneBinding
import com.bawei.shopping2.entity.CategoryEntity
import com.bawei.shopping2.entity.GoodsEntity
import com.bawei.shopping2.http.ApiResponse
import com.bawei.shopping2.utils.ArouterPath
import com.bawei.shopping2.viewmodel.MainViewModel
import com.bawei.shopping2.widget.MyItemDecoration
import com.blankj.utilcode.util.AdaptScreenUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.*

@AndroidEntryPoint
class OneFragment : Fragment() {
    private lateinit var fragmentOneBinding: FragmentOneBinding
    private val mainViewModel:MainViewModel by activityViewModels()
    private val job = Job()
    private lateinit var mainCategoryAdapter: MainCategoryAdapter
    private var data:MutableList<CategoryEntity> = mutableListOf()//数据源

    private lateinit var categoryAdapter: CategoryAdapter
    private var data2:MutableList<CategoryEntity> = mutableListOf()//数据源


    private lateinit var goodsAdapter: GoodsAdapter
    private var goodsData:MutableList<GoodsEntity> = mutableListOf()//数据源

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        fragmentOneBinding = FragmentOneBinding.inflate(inflater,container,false)
        //初始化一级分类的列表
        initMainCategory()
        //初始化二级分类的列表
        initCategory()
        //初始化商品列表
        initGoods()
        //使用协程: 优雅实现异步任务
        CoroutineScope(job).launch(Dispatchers.Main) {
            val apiResponse = mainViewModel.mainCategory()//请求一级分类数据
            showMainCategory(apiResponse)//展示一级分类列表
        }
        //点击一级分类事件使用协程: 优雅实现异步任务
        mainCategoryAdapter.onItemClick = {v: View, pos: Int->
            var  id = mainCategoryAdapter.getDataByPosition(pos).id//获得id
            CoroutineScope(job).launch(Dispatchers.Main) {
                val apiResponse = mainViewModel.category(id)//根据id请求二级分类数据
                showCategory(apiResponse)//展示二级分类列表
            }

        }
        //点击二级分类事件使用协程: 优雅实现异步任务
        categoryAdapter.onItemClick = {v: View, pos: Int->
           var id = categoryAdapter.getDataByPosition(pos).id
            CoroutineScope(job).launch(Dispatchers.Main) {
                val apiResponse =  mainViewModel.goods(id,1,10)//根据分类id请求商品数据
                showGoods(apiResponse)//展示商品数据
            }
        }
        //点击商品
        goodsAdapter.onItemClick = {v: View, pos: Int->
            ARouter.getInstance().build(ArouterPath.ROUTE_DETAIL_PATH)
                .withInt("id",goodsAdapter.getDataByPosition(pos).id)//商品id
                .navigation();

        }

        return fragmentOneBinding.root
    }

    private fun initGoods() {
        goodsAdapter = GoodsAdapter(goodsData)
        fragmentOneBinding.rvGoods.adapter = goodsAdapter
        fragmentOneBinding.rvGoods.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        fragmentOneBinding.rvGoods.addItemDecoration(MyItemDecoration(15,2))//dp转px
    }

    private fun showGoods(apiResponse: ApiResponse<MutableList<GoodsEntity>>) {
        if(apiResponse.code == 200){
            goodsData.addAll(apiResponse.data)
            goodsAdapter.notifyDataSetChanged()
        }

    }

    private fun initCategory() {
        categoryAdapter = CategoryAdapter(data2)
        fragmentOneBinding.rvCategory.adapter = categoryAdapter
        //横向滑动的recyclerview
        var  linearLayoutManager = LinearLayoutManager(context)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        fragmentOneBinding.rvCategory.layoutManager = linearLayoutManager

    }

    private fun showCategory(apiResponse: ApiResponse<MutableList<CategoryEntity>>) {
        if(apiResponse.code == 200){
            data2.clear()
            data2.addAll(apiResponse.data)
            categoryAdapter.notifyDataSetChanged()
        }

    }

    private fun initMainCategory() {
        mainCategoryAdapter = MainCategoryAdapter(data)
        fragmentOneBinding.rvMainCategory.adapter = mainCategoryAdapter
        fragmentOneBinding.rvMainCategory.layoutManager = LinearLayoutManager(context)
        fragmentOneBinding.rvMainCategory.addItemDecoration(DividerItemDecoration(context,LinearLayoutManager.VERTICAL))

    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }
    //展示一级分数数据
    fun showMainCategory(apiResponse:ApiResponse<MutableList<CategoryEntity>>){
        if(apiResponse.code == 200){
            data.addAll(apiResponse.data)
            mainCategoryAdapter.notifyDataSetChanged()
        }

    }

}